package controller;

import dao.MemberAdminDao;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import model.MemberAdmin;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Objects;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


@WebServlet(name = "AdminServlet", value = "/admin/AdminService")
public class AdminServlet extends HttpServlet {
    public static final Logger logger = LogManager.getLogger(AdminServlet.class);

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getParameter("method") == null ? "" : request.getParameter("method");
        MemberAdminDao memberAdminDao = new MemberAdminDao();

        // 管理员登录
        if ("login".equals(method)) {
            MemberAdmin memberAdmin = (MemberAdmin) request.getAttribute("login");
            try {
                String codeValidate;
                if (request.getSession().getAttribute("codeValidate") != null) {
                    codeValidate = (String) request.getSession().getAttribute("codeValidate");
                } else {
                    codeValidate = "Error";
                }
                // 获取用户提交验证码
                String validateCode = request.getParameter("validateCode");
                // 验证码转小写并比较是否相等
                logger.info("Session.validateCode:" + codeValidate + " getParameter.validateCode:" + validateCode);
                assert codeValidate != null;
                if (!Objects.equals(codeValidate.toLowerCase(), validateCode.toLowerCase())) {
                    logger.info("Admin login validateCode Error");
                    request.getSession().setAttribute("admin_login_error", "验证码错误");
                    response.sendRedirect("/admin/login.jsp");
                } else if (memberAdminDao.Login(memberAdmin)) {
                    request.getSession().setAttribute("admin_username", memberAdmin.getUsername());
                    response.sendRedirect("/admin/index.jsp");
                } else {
                    logger.info("admin login error by:" + memberAdmin.getUsername());
                    request.getSession().setAttribute("admin_login_error", "请检查用户名和密码是否正确");
                    response.sendRedirect("/admin/login.jsp");
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

        // 管理员注销
        if ("logout".equals(method)) {
            if (request.getSession().getAttribute("admin_username") != null) {
                logger.info("Now admin:" + request.getSession().getAttribute("admin_username") + " logout!");
                request.getSession().removeAttribute("admin_username");
            }
            response.sendRedirect("/admin/login.jsp");
        }
    }
}
